package com.h3bpm.kumhosunny.purchase.controller;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import OThinker.H3.Portal.webservices.BPMService;
import OThinker.H3.Portal.webservices.Entity.BPMServiceResult;
import OThinker.H3.Portal.webservices.Entity.DataItemParam;
import OThinker.H3.Portal.webservices.impl.BPMServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @authormi
 * @date2019/8/19
 */

@Controller
@RequestMapping("/Portal/AcceptanceSummaryChild")
public class AcceptanceSummaryChild extends ControllerBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBase.class);

    private BPMService bpmService = new BPMServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public String getFunctionCode() {
        return null;
    }


@RequestMapping("/startAcceptanceSummaryChild")
@ResponseBody
 public String startAcceptanceSummaryChild (String instanceId)  {
    String result ="200";
    try {
        InstanceData instanceData = new InstanceData(getEngine(), instanceId, User.AdministratorID);
        HashMap<String, Object> valueTable = instanceData.getBizObject().getValueTable();
        Map<String, List<DataItemParam>> dataMap =   dataProcessing(  instanceId,  valueTable);

        for(String user : dataMap.keySet()){
        LOGGER.info("  发起子流程=========发起人:" +user);
        BPMServiceResult bpmServiceResult = bpmService.StartWorkflow("H3", "Authine", "acceptanceSummaryNotice", user, false, dataMap.get(user));
        LOGGER.info(">> " + objectMapper.writeValueAsString(bpmServiceResult));
        }
    }catch(Exception e){
          result="500";
         e.printStackTrace();

    }

    return result;
 }

  public Map<String, List<DataItemParam>>  dataProcessing(String instanceId,HashMap<String, Object> valueTable) throws Exception {

      Map<String, List<DataItemParam>> dataMap = new HashMap<>  (3);
      String[] technologyLeaders = ( String[]) valueTable.get("technologyLeader");
      String[]   businessLeader = ( String[]) valueTable.get("businessLeader");
      String[]  projectLeader = ( String[]) valueTable.get("projectLeader");
      String[]  acceptanceleader = ( String[]) valueTable.get("acceptanceleader");
      String[]  projectMember = ( String[]) valueTable.get("projectMember");
      String[]  technologyMember = ( String[]) valueTable.get("technologyMember");
      String[]  businessMember = ( String[]) valueTable.get("businessMember");


      List<String> userStr = new ArrayList<String>();

      if(projectMember!=null && projectMember.length>0){
          for(String str : projectMember){
              userStr.add(str);
          }
      }

      if(technologyMember!=null && technologyMember.length>0){
          for(String str : technologyMember){
              userStr.add(str);
          }
      }

      if(businessMember!=null && businessMember.length>0){

          for(String str : businessMember){
              userStr.add(str);
          }
      }




      if(technologyLeaders!=null && technologyLeaders.length>0){
          userStr.add(technologyLeaders[0]);
      }

      if(businessLeader!=null && businessLeader.length>0){
          userStr.add(businessLeader[0]);
      }

      if(projectLeader!=null && projectLeader.length>0){
          userStr.add(projectLeader[0]);
      }

      for(String str : acceptanceleader){
          userStr.add(str);

      }

      for(String user : userStr) {

          List<DataItemParam> paramValues = new ArrayList<>();
          User crateBy = (User) getEngine().getOrganization().GetUnit(user);
          paramValues.add(new DataItemParam("projectName", valueTable.get("projectName")));
          paramValues.add(new DataItemParam("projectNumber", valueTable.get("projectNumber")));
          paramValues.add(new DataItemParam("ParentObjectID", valueTable.get("ObjectID")));
          dataMap.put(crateBy.getCode(),paramValues);

      }

      return dataMap;
  }



}
